﻿
var islogin = false;
//获取用户是否登录 初始化聊天窗体
function loginUserInfo() {
    jQuery.loadHtml('/pgajax.axd?GET_USER_INFO=ok').done(function (_rets) {
        _rets = JSON.parse(_rets);
        if (_rets.records <= 0) { return false; }
        //var usrinfo = _rets.rows;
        xxim.Initialization(_rets.CUSTOMER_REGISTER_ID, _rets.CUSTOMER_NICK, _rets.CUSTOMER_DEFAULT_PIC, { records: 0 });
        xxim.view();
        xxim.renode();
        xxim.getDates(0);
        xxim.event();
        xxim.layinit();
        if ($("#receiverInfo").length > 0) {
            $("#receiverInfo").attr("lg", "true");
        }
        islogin = true;
        //获取历史聊天人
        getChatRecently();
        //getGroup();
        return true;
    });
}
//获取消息 分组列队
function getGroup() {
    jQuery.loadHtml('/pgajax.axd?GET_GROUP=ok').done(function (_rets) {
        _rets = JSON.parse(_rets);
        xxim.messageInitia(_rets);
    });
}
//获取最近聊天人
function getChatRecently() {
    jQuery.loadHtml('/pgajax.axd?GET_RRCENTLY=ok').done(function (_rets) {
        _rets = JSON.parse(_rets);
        xxim.messageInitia(_rets);
    });
}

//消息发送
function senderMessage(senderNick, reciverid, receiverNick, msg) {
    var url = "&SENDER_NICK=" + senderNick + "&RECIVERID=" + reciverid + "&RECEIVER_NICK=" + receiverNick + "&MESSAGE=" + msg + "";
    jQuery.loadHtml('/pgajax.axd?SENDER_MSG=ok' + url).done(function (_rets) {
        _rets = JSON.parse(_rets);
        if (_rets.status == 1) {
            xxim.sendMessage();
        }
        else {
            layer.msg('消息发送失败，请重新发送。', 3, { shift: 6 });
        }
    });
}
//消息接收
function getMessage(senderid,status) {
    jQuery.loadHtml('/pgajax.axd?ACCEPT_MSG=ok&STATUS=' + status + '&SENDERID=' + senderid).done(function (_rets) {
        _rets = JSON.parse(_rets);
        if (_rets.records > 0) {
            xxim.getMessage(_rets);
        }
    });
}
//点击咨询 加载事件
function chatShow(e) {
    var islog = $(e).attr("lg");
    if (islog == "false") {
        layer.msg('请先登录后才能咨询哦！', 3, { shift: 6 });
        return;
    }
    var userid = $(e).attr("userid");
    var username = $(e).attr("username");
    var userface = $(e).attr("userface");
    var friend = { page: 1, total: 1, records: 1, rows: [{ id: userid, name: username, face: userface, msgCount: 0 }] };
    var isInitia = xxim.messageInitia(friend);
    if (isInitia) {
        xxim.node.list.eq(0).find("ul li").each(function (i,e) {
            if ($(e).attr("data-id") == userid) {
                $(e).click();
                return false;
            }
        });
    }
}

setTimeout(function () {
    //初始化聊天窗体
    loginUserInfo();
    
    //定时获取消息 条数
    setInterval(function () {
        if (islogin) {
            getGroup();
        }
    }, 10000);

}, 1000);